#include<iostream>
#include<mutex>
using namespace std;

//饿汉模式
namespace Singleton1
{
    class Singleton
    {
    private:
        static Singleton _eton;//类内静态成员
        Singleton():_data(99)
        {
            cout << "获取单例..." << endl;
        }
        ~Singleton(){};
        Singleton(const Singleton& sig) = delete;
    private:
        int _data;      
    public:
        static Singleton& GetInstance()
        {
            return _eton;
        }      
        int GetData()
        {return _data;}
    };

    Singleton Singleton::_eton; //类外真正定义类内静态成员
}

//懒汉模式线程安全版
namespace Singleton2
{
class Singleton
{
private:
     static Singleton* _eton;
    static std::mutex _lock;
    Singleton():_data(99)
    {
       cout << "获取单例..." << endl;
    }
    Singleton(const Singleton& sig) = delete;
private:
    int _data;        
public:
    static Singleton* GetInstance()
    {
        if(_eton == nullptr)
        {
            _lock.lock();
            if(_eton == nullptr)
            {
                _eton = new Singleton();
            }
            _lock.unlock();
        }
        return _eton;
    }
    int GetData()
    {return _data;}
};

 Singleton* Singleton::_eton = nullptr;
 std::mutex Singleton::_lock;
}

//懒汉模式C++11静态对象版
class Singleton
{
private:
     Singleton():_data(99)
     {
        cout << "获取单例..." << endl;
     }
     ~Singleton(){};
     Singleton(const Singleton& sig) = delete;
private:
     int _data;
public: 
    static Singleton& GetInstance()
    {
        static Singleton _eton;
        return _eton;
    }        
    
    int GetData()
    {
        return _data;
    }

};


int main()
{
    cout << Singleton::GetInstance().GetData() << endl;
   return 0;
}